package com.zkingsoft.actions.admin.pc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.annotation.Resource;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.xml.crypto.Data;

import com.zkingsoft.services.bus.BusParameterSettingsService;
import com.zkingsoft.services.pc.PcMenuDetailService;
import com.zkingsoft.services.pc.PcMenuRecordService;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.bus.BusParameterSettings;
import com.zkingsoft.model.pc.PcMenuRecord;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.authority.MatrixAdapter;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.util.WebUtil;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.constraint.BaseController;
import com.zkingsoft.pojo.AjaxResult;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zkingsoft.anotations.RemoveRequestToken;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-03-23 15:08
 */
@Controller
@RequestMapping(value = "admin/pcMenuRecord")
public class PcMenuRecordController extends BaseController{

	@Resource
	private PcMenuRecordService pcMenuRecordService;
	
	@Resource
	private BusParameterSettingsService busParameterSettingsService;
	
	@Resource
	private PcMenuDetailService pcMenuDetailService;
	@Resource
	private MatrixAdapter matrixAdapter;
	
	
	Logger log = Logger.getLogger(this.getClass());
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="PcMenuRecord_BEV";
	
	public static final String fnCode = "PcMenuRecord";
	public static final String search = fnCode + ":search";
	public static final String edit = fnCode + ":edit";
	public static final String del = fnCode + ":del";
	public static final String add = fnCode + ":add";
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(PcMenuRecord pcMenuRecord, PaginationVO pageVo) {
		matrixAdapter.isBtnPermitted(search);
		return showList(pcMenuRecordService, pcMenuRecord, pageVo);
	}
	/**
	 * 设置月份周数
	 */
	@RequestMapping(value = "/setWeeks")
	public  @ResponseBody AjaxResult setWeeks(String mouth,String week) {
		WebUtil.getRequest().setAttribute("mouth", mouth+"-"+week);
		return new AjaxResult(AjaxResult.STATUS_OK, Arrays.asList(mouth+"-"+week));
	}
   	
	/**
	 * 生成本周菜谱
	 */   
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(PcMenuRecord pcMenuRecord) {
   		SysUsers users=(SysUsers) WebUtil.getSessionAttribute(SystemConstance.LOGIN_KEY);
   		pcMenuRecord.setMenUserId(users.getSuId());
		String weekSign = pcMenuRecord.getMenWeekSign();
		PcMenuRecord record = new PcMenuRecord();
		record.setMenWeekSign(weekSign);
		List<PcMenuRecord>pcMenuRecordList = pcMenuRecordService.findByModel(record);
		int listSize=pcMenuRecordList.size();
		BusParameterSettings bus1 = new BusParameterSettings();
		List<BusParameterSettings> list1 =  busParameterSettingsService.findByModel(null);
		bus1 = list1.get(0);
		if(listSize>0){
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "所选月份周数已生成菜谱");
		}else{
			pcMenuRecord.setMenManMun(Long.parseLong(bus1.getParamValue()));
			return add(pcMenuRecordService, pcMenuRecord, "生成菜谱成功");
		}
	}
	
	/**
	 * 进入修改界面
	 */
   	@RequestMapping(value = "/editForm")
	public String editForm() {
		matrixAdapter.isBtnPermitted(edit);
		return "admin/pc/PcMenuRecord-form";
	}
   	
   	
	/**
	 * 删除
	 */  
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		matrixAdapter.isBtnPermitted(del);
		return remove(pcMenuRecordService, keys);
	}
 	
 	/**
	 * 根据时间查找时间段内的记录
	 */  
 	@RequestMapping(value = "/findByTime")
	public @ResponseBody AjaxResult findByTime(Date startTime,Date endTime) {
 		Date startTime1=(Date) WebUtil.getRequestAttribute("beginTime");
 		Date endTime1=(Date) WebUtil.getRequestAttribute("endTime");
 		AjaxResult result = new AjaxResult(AjaxResult.STATUS_OK, null, null,pcMenuRecordService.findByTime(startTime1, endTime1), 0);
		return result;
	}
  
}